//
//  ChooseSexViewController.m
//  SmartCup
//
//  Created by Kendama on 2017/5/16.
//  Copyright © 2017年 DT. All rights reserved.
//

#import "ChooseSexViewController.h"

@interface ChooseSexViewController ()
//@property (nonatomic, strong) UIImageView *manView;
//@property (nonatomic, strong) UIImageView *womanView;
@property (nonatomic, strong) UIButton *nextButton;
@property (nonatomic, strong) UIButton *boyButton;
@property (nonatomic, strong) UIButton *grilButton;

/** 性别 **/
@property(nonatomic,copy) NSString *sex;

@end

@implementation ChooseSexViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.sex = @"1";
}

- (void)refreshBaseControlValue
{
    if (![DTCEXRACT_OBJECT(REGISTER) isEqualToString:@"2"]) {
        [self setLeftButtonImage:[UIImage imageNamed:@"nav_back_white_nor"]];
    }
//    [self setLeftButtonImage:[UIImage imageNamed:@"nav_back_white_nor"]];
    [self hideNavigationBottomLine];
    [self setNavigationTitle:@"选择您的性别" titleColor:[UIColor whiteColor]];
    self.view.backgroundColor = [UIColor colorWithHexString:@"3E76F6"];
    
    [self layoutSubviews];
    //    [self.view addSubview:self.iconImageView];
}

- (void) layoutSubviews{
    [self.boyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(px1920Height(273));
        make.centerX.equalTo(self.view);
        make.width.height.mas_equalTo(px1080Width(326));
    }];
    
    [self.grilButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.boyButton.mas_bottom).with.offset(px1920Height(157));
        make.centerX.equalTo(self.view);
        make.width.height.mas_equalTo(px1080Width(326));
    }];
    
    [self.nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.grilButton.mas_bottom).with.offset(px1920Height(455));
        make.left.equalTo(self.view).with.offset(px1080Width(100));
        make.right.equalTo(self.view).with.offset(-px1080Width(100));
        make.height.mas_equalTo(px1920Height(136));
    }];
}


#pragma mark -- 懒加载
- (UIButton *)boyButton
{
    if(!_boyButton)
    {
        _boyButton = [[UIButton alloc]init];
//        [_boyButton setTitleColor:[UIColor colorWithHexString:@"#3E76F6"] forState:UIControlStateNormal];
        _boyButton.layer.masksToBounds = YES;
        _boyButton.layer.cornerRadius = px1080Width(326) / 2 ;
        //        [_registerButton.titleLabel setFont:<#(UIFont * _Nullable)#>];
        [_boyButton setBackgroundImage:[UIImage imageNamed:@"button_boy_sel"] forState:UIControlStateNormal];
        [_boyButton setBackgroundImage:[UIImage imageNamed:@"button_boy_nor"] forState:UIControlStateSelected];
        [_boyButton addTarget:self action:@selector(didClickBoyButton) forControlEvents:UIControlEventTouchUpInside];
        _boyButton.selected = YES;
        [self.view addSubview:_boyButton];
    }
    return _boyButton;
}

- (UIButton *)grilButton
{
    if(!_grilButton)
    {
        _grilButton = [[UIButton alloc]init];
//        [_grilButton setTitleColor:[UIColor colorWithHexString:@"#3E76F6"] forState:UIControlStateNormal];
        _grilButton.layer.masksToBounds = YES;
        _grilButton.layer.cornerRadius = px1080Width(326) / 2 ;
        //        [_registerButton.titleLabel setFont:<#(UIFont * _Nullable)#>];
        [_grilButton setBackgroundImage:[UIImage imageNamed:@"button_girl_sel"] forState:UIControlStateNormal];
        [_grilButton setBackgroundImage:[UIImage imageNamed:@"button_girl_nor"] forState:UIControlStateSelected];
        [_grilButton addTarget:self action:@selector(didClickGirlButton) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_grilButton];
    }
    return _grilButton;
}


- (UIButton *)nextButton
{
    if(!_nextButton)
    {
        _nextButton = [[UIButton alloc]init];
        [_nextButton setTitle:@"下一步" forState:UIControlStateNormal];
        [_nextButton setTitleColor:[UIColor colorWithHexString:@"#3E76F6"] forState:UIControlStateNormal];
        //        [_registerButton.titleLabel setFont:<#(UIFont * _Nullable)#>];
        [_nextButton setBackgroundImage:[UIImage imageNamed:@"button_next_nor"] forState:UIControlStateNormal];
        [_nextButton setBackgroundImage:[UIImage imageNamed:@"button_next_sel"] forState:UIControlStateHighlighted];
        [_nextButton addTarget:self action:@selector(didClickNextButton) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_nextButton];
    }
    return _nextButton;
}


#pragma mark -- 事件
- (void)didClickNextButton {
    if (self.sex == 0) {
        [SVProgressHUD showErrorWithStatus:@"请选择性别"];
        return;
    }
    
    [self updateUserInfo];
}

- (void)updateUserInfo
{
    SetUserInfoApi *api = [[SetUserInfoApi alloc]initWithInfo:@{@"sex":self.sex}];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSDictionary *dic = request.responseJSONObject;
        if ([dic[@"errcode"]intValue] == 0) {
            GetUserInfoApi * userInfoApi = [[GetUserInfoApi alloc] init];
            [userInfoApi startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
                [userInfoApi analyseResponseObject];
                DTCSAVE_OBJECT(@"YES", LOGIN);
                [[AppDelegate defaultService]setRootViewController];
//                SeekMachineViewController *vc = [[SeekMachineViewController alloc] init];
//                [self.navigationController pushViewController:vc animated:YES];
            } failure:^(YTKBaseRequest *request) {
                [SVProgressHUD showErrorWithStatus:@"请求超时"];
            }];
        }
    } failure:^(YTKBaseRequest *request) {
        
    }];
}

- (void)didClickBoyButton {
    self.sex = @"1";
    self.boyButton.selected = YES;
    self.grilButton.selected = NO;
}

- (void)didClickGirlButton {
    self.sex = @"2";
    self.boyButton.selected = NO;
    self.grilButton.selected = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
